package com.nf.health.app.activity.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.adapter.MyQuestionResultAdapter;
import com.nf.health.app.activity.my.bean.QuestionResultModel;
import com.nf.health.app.activity.my.impl.MyContact;
import com.nf.health.app.customview.MyExpandableListView;
import com.nf.health.app.http.Act;
import com.nf.health.app.impl.event.EventContact;
import com.nf.health.app.impl.event.EventType;
import com.nf.health.app.utils.ViewUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 答题结果
 */

public class HousePersionCompeteResultActivity extends BaseActivity {
    String  myAnswer = "";
    @Bind(R.id.tv_socre_persion)
    TextView tv_socre_persion;
    @Bind(R.id.lv_healthi_compete_result)
    MyExpandableListView lv_healthi_compete_result;
    @Bind(R.id.frm_socre_persion)
    FrameLayout frm_socre_persion;
    @Bind(R.id.tv_show_compete_result)
    TextView tv_show_compete_result;
    @Bind(R.id.titlebar_back_view)
    ImageView titlebar_back_view;
    List<QuestionResultModel.ListBean> list2;
    private MyQuestionResultAdapter myQuestionExpandableAdapter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_housepersioncompeteresult);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "答题结果");
        regisitListener();
        initView();
        loadData(myAnswer);
    }

    private void loadData(String answer) {
        if(!TextUtils.isEmpty(answer)){
            provider.commitCompeteListNewQUestion(answer);
        }
    }

    private void regisitListener() {
        titlebar_back_view.setOnClickListener(this);
        lv_healthi_compete_result.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
    }

    private void initView() {
        myAnswer = (String) getIntent().getSerializableExtra(MyContact.COMMIT_TO_SENSD_FLAG);
        tv_socre_persion.setText("");
        tv_show_compete_result.setText("");

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.titlebar_back_view:
                EventType eventType = new EventType();
                EventBus.getDefault().postSticky(eventType);
                break;
        }
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.FLAG_COMMIT_HOURSE_RESPONSIBLE_GETNEWINQUIRYLIST:
                QuestionResultModel questionResultModel = (QuestionResultModel) object;
                if(questionResultModel != null){
                    try {
                        tv_socre_persion.setText(TextUtils.isEmpty(questionResultModel.getScore()) ? "" : questionResultModel.getScore());
                        List<QuestionResultModel.ListBean> list = questionResultModel.getList();
                        list2 = new QuestionResultModel().parse2(list);
                        myQuestionExpandableAdapter = new MyQuestionResultAdapter(list2,this);
                        lv_healthi_compete_result.setAdapter(myQuestionExpandableAdapter);
                        for (int i = 0; i < list.size(); i++) {
                            lv_healthi_compete_result.expandGroup(i);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }


    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
        switch (actionName){
            case Act.Flag.FLAG_COMMIT_HOURSE_RESPONSIBLE_GETNEWINQUIRYLIST:

                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        EventType eventType = new EventType();
        eventType.setType( EventContact.QUESTION_ADK);
        EventBus.getDefault().postSticky(eventType);
    }
}
